﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Koala.Pro.Common;
using Koala.Pro.Core.Models;
using Koala.Pro.Helpers;
using Koala.Pro.Models;
using Koala.Pro.Services;
using System.Collections.ObjectModel;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace Koala.Pro.ViewModels
{
    public partial class MainWindowViewModel : ObservableObject
    {
        [ObservableProperty]
        private bool notifyIconIsShow = true;

        [ObservableProperty]
        private bool contextMenuIsShow = true;

        [ObservableProperty]
        private bool contextMenuIsBlink;

        [ObservableProperty]
        private bool canBack = true;

        [ObservableProperty]
        private bool isDirty;

        [ObservableProperty]
        private bool disableDirtyMessage = true;

        [ObservableProperty]
        private Type currentPageOrWinType;

        [ObservableProperty]
        private UserInfo userInfo;

        [ObservableProperty]
        private List<RoleInfo> roleList = new List<RoleInfo>();

        [ObservableProperty]
        private List<FunctionInfo> funcList = new List<FunctionInfo>();

        [ObservableProperty]
        private string systemType = "Sugar-WPF";

        [ObservableProperty]
        private bool isUserAdmin;

        [ObservableProperty]
        private bool isSuperAdmin;

        [ObservableProperty]
        private string _applicationTitle = string.Empty;

        [ObservableProperty]
        private ObservableCollection<NavigationViewItem> _navigationItems = new ObservableCollection<NavigationViewItem>();

        [ObservableProperty]
        private ObservableCollection<NavigationViewItem> _navigationFooter = new ObservableCollection<NavigationViewItem>();

        [ObservableProperty]
        private ObservableCollection<MenuItem> _trayMenuItems = new ObservableCollection<MenuItem>();

        [ObservableProperty]
        private ObservableCollection<string> _BreadcrumbItems = new ObservableCollection<string>();

        private INavigationService navigationService;

        public MainWindowViewModel(INavigationService navigationService)
        {
            this.navigationService = navigationService;
            ApplicationTitle = "SqlSugar框架的WPF应用端";
            WeakReferenceMessenger.Default.Register(this, delegate (object r, UserLoginMessage m)
            {
                GrowlUtil.ShowSuccess("用户【" + m.Value.FullName + "】登录系统成功 ");
            });
        }

        public async Task GetRemoteMenus()
        {
            List<MenuInfo> menuList = await BLLFactory<IMenuService>.Instance.GetMenuNodesByUser(UserInfo.Id, SystemType);
            if (menuList.Count != 0)
            {
                NavigationItems = AddMenuItem(menuList);
                OnPropertyChanged("NavigationItems");
            }
        }

        private ObservableCollection<NavigationViewItem> AddMenuItem(List<MenuInfo> list, NavigationViewItem topMenu = null)
        {
            var navItemList = new ObservableCollection<NavigationViewItem>();
            foreach (MenuInfo item in list)
            {
                NavigationViewItem navigationViewItem = new NavigationViewItem
                {
                    Content = item.Name,
                    IsExpanded = (item.Expand == 1),
                    TargetPageType = WpfTypeHelper.LoadWpfType(item.WpfType)
                };
                if (Enum.TryParse<SymbolRegular>(item.Icon, out var result))
                {
                    navigationViewItem.Icon = new SymbolIcon
                    {
                        Symbol = result
                    };
                }
                AddMenuItem(item.Children, navigationViewItem);
                navItemList.Add(navigationViewItem);
            }
            if (topMenu != null)
            {
                topMenu.MenuItems = navItemList;
                return new ObservableCollection<NavigationViewItem> { topMenu };
            }
            return navItemList;
        }
    }
}
